package com.ruoyi.common.tools.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.ruoyi.api.dto.StatDateEnumDto;

public class DateUtils {
	private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

	/**
	 * if date1 > date2 get date1 , else get data2
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static String getDate(String date1, String date2) {
		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			calendar1.setTime(df.parse(date1));
			calendar2.setTime(df.parse(date2));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}

		if (calendar1.compareTo(calendar2) >= 0) {
			return df.format(calendar1.getTime());
		}

		return df.format(calendar2.getTime());
	}

	/**
	 * date1 >= date2 = true . date1 == null is return true
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareDate(String date1, String date2) {
		if (StringUtils.isEmpty(date1)) {
			return true;
		}

		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			calendar1.setTime(df.parse(date1));
			calendar2.setTime(df.parse(date2));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
			return false;
		}

		return (calendar1.compareTo(calendar2) >= 0);
	}

	/**
	 * date1 >= date2 = true . date1 == null is return true
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareDate2(String date1, String date2) {
		if (StringUtils.isEmpty(date1)) {
			return true;
		}

		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			calendar1.setTime(df.parse(date1));
			calendar2.setTime(df.parse(date2));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
			return false;
		}

		return (calendar1.compareTo(calendar2) >= 0);
	}

	/**
	 * currTime > date + number(hour) = true
	 * 
	 * @param date
	 * @param number
	 * @return
	 */
	public static boolean compareCurrTime(String date, int number) {
		Calendar curr = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			calendar2.setTime(df.parse(date));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}

		calendar2.add(Calendar.HOUR, number);

		return (curr.compareTo(calendar2) > 0);
	}

	public static long getDateTimeBySubCurrTime(String dateTime, int number) {
		Calendar curr = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			calendar2.setTime(df.parse(dateTime));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}

		calendar2.add(Calendar.DAY_OF_YEAR, number);

		long tmp = calendar2.getTimeInMillis() - curr.getTimeInMillis();

		if (tmp < 0) {
			tmp = 0;
		}

		return tmp;
	}

	public static String getPreDate(int subDay) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - subDay);

		String d = df.format(calendar.getTime());

		return d;
	}

	public static String getNextDate(String date, int addDay) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(df.parse(date));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + addDay);

		String d = df.format(calendar.getTime());

		return d;
	}

	public static String getNextDate(String date, int addDay, String retFormat) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtils.isEmpty(retFormat))
			retFormat = "yyyy-MM-dd";
		DateFormat df1 = new SimpleDateFormat(retFormat);

		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(df.parse(date));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + addDay);

		String d = df1.format(calendar.getTime());

		return d;
	}

	public static String getFormatDate(String date, String curFormat, String retFormat) {
		DateFormat df = new SimpleDateFormat(curFormat, Locale.ENGLISH);
		DateFormat df1 = new SimpleDateFormat(retFormat, Locale.ENGLISH);

		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(df.parse(date));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}

		String d = df1.format(calendar.getTime());

		return d;
	}

	public static String getCurrDate() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		String d = df.format(calendar.getTime());

		return d;
	}

	public static String getCurrDate(String format) {
		DateFormat df = new SimpleDateFormat(format);
		Calendar calendar = Calendar.getInstance();
		String d = df.format(calendar.getTime());

		return d;
	}

	public static String getDate(String format, String format1, String dateStr) {
		String d = null;
		DateFormat df = new SimpleDateFormat(format);
		DateFormat df1 = new SimpleDateFormat(format1);
		Date date = null;
		try {
			date = df.parse(dateStr);
			d = df1.format(date);
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}
		return d;
	}

	public static String getCurrDateTime() {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		String d = df.format(calendar.getTime());

		return d;
	}

	public static String getDateTime(Date date) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String d = df.format(calendar.getTime());

		return d;
	}


	public static String getDate(Date date) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String d = df.format(calendar.getTime());

		return d;
	}
	
	public static StatDateEnumDto getStatDateEnumDto(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return new StatDateEnumDto(String.valueOf(calendar.get(Calendar.YEAR)),
				String.valueOf(calendar.get(Calendar.MONTH) + 1), String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
	}

	public static void main(String[] args) {
		// System.out.println(compareDate("2014-05-10 00:00:00", StringUtil.getDate()));
		// System.out.println(getDateTimeBySubCurrTime("2014-05-31 00:00:00", 1));
		System.out.println(getPreDate(30));
	}
}
